package controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import entity.Users;
import entity.VideoList;
import service.UsersService;
import service.VideoListService;
import service.VideoService;

@Controller
@RequestMapping("/videoList")
public class VideoListController {
	
	@Autowired
	UsersService usersService;
	@Autowired
	VideoListService videoListService;
	
	//根据用户的编号查找播单
	@RequestMapping("/singleSowing")
	public String findSingSowing(Model model)
	{
		List<VideoList> list = videoListService.findByRecentList();
		model.addAttribute("list", list);
		return "/page/video_list";
	}
	
	//添加一个播单
	@RequestMapping("/add")
	public String add(Model model, VideoList videoList, HttpServletRequest request) {
		
		List<String> filenames = new ArrayList<>();
		
		Date date = new Date();
		String username = (String) request.getSession().getAttribute("username");//获取当前登陆用户的账号
		Users user = usersService.findByUserName(username);
		
		videoList.setUser(user);
		videoList.setUploadTime(new Date());
		
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		if(multipartResolver.isMultipart(request)) {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			Iterator files = multipartRequest.getFileNames();
		
			
			while(files.hasNext()) {
				MultipartFile file = multipartRequest.getFile(files.next().toString());
				System.out.println("获取文件名字:"+file.getOriginalFilename());
				String originalFilename = file.getOriginalFilename();
				// 截取文件名字：---a.jpg
				String substring = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);//获取文件名.的后缀名
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
				String s=sdf.format(date);
				String lastname=s + "-" + username+"."+substring;
				
				System.out.println("打印最后修改的名字"+lastname);
				
				
				if(!file.getContentType().startsWith("image")) {
					continue;
				}
				if(!file.getOriginalFilename().equals("")) {
					String path = request.getSession().
									getServletContext().
									getRealPath("/upload/head-icon") + "\\" + lastname;
					String path_base = path.substring(path.indexOf("upload"));//这个path_bass就是存在数据库里面的picUrl
					
					try {
						file.transferTo(new File(path));
						
						videoList.setImg_url(path_base);
						videoListService.add(videoList);   //添加一个播单
						
					} catch (Exception e) {
						System.out.println(e.getMessage());
					}
				}
			}
		}
		return "redirect:/users/update?username=" + user.getUsername();
	}
}
